package logic

import (
	"context"

	"go-weixin/app/usercenter/rpc/internal/domain"
	"go-weixin/app/usercenter/rpc/internal/svc"
	"go-weixin/app/usercenter/rpc/pb/proto/usercenter"

	"github.com/zeromicro/go-zero/core/logx"
)

type RoleLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
	roleDomain *domain.RoleDomain
}

func NewRoleLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RoleLogic {
	return &RoleLogic{
		ctx:        ctx,
		svcCtx:     svcCtx,
		Logger:     logx.WithContext(ctx),
		roleDomain: domain.NewRoleDomain(*svcCtx.ChenDb),
	}
}

func (l *RoleLogic) RoleAdd(in *usercenter.RoleAddReq) (*usercenter.RoleAddResp, error) {
	// 查询角色名是否被注册
	err := l.roleDomain.RoleExistByName(in.Name)
	if err != nil {
		return nil, err
	}

	err = l.roleDomain.RoleAdd(in.Name, in.Weight, in.ParentId)
	if err != nil {
		return nil, err
	}
	return &usercenter.RoleAddResp{}, nil
}

func (l *RoleLogic) RoleUpdate(in *usercenter.RoleUpdateReq) (*usercenter.RoleUpdateResp, error) {
	err := l.roleDomain.RoleUpdateExist(in.Name, in.Id)
	if err != nil {
		return nil, err
	}

	err = l.roleDomain.RoleUpdate(in.Id, in.Name, in.ParentId, in.Weight)
	if err != nil {
		return nil, err
	}
	return &usercenter.RoleUpdateResp{}, nil
}

func (l *RoleLogic) RoleAddPower(in *usercenter.RoleAddPowerReq) (*usercenter.RoleAddPowerResp, error) {
	err := l.roleDomain.RoldAddPowerExist(in.RoleId, in.PowerId)
	if err != nil {
		return nil, err
	}
	err = l.roleDomain.RoleAddPower(in.RoleId, in.PowerId)
	if err != nil {
		return nil, err
	}
	return &usercenter.RoleAddPowerResp{}, nil
}

func (l *RoleLogic) RoleUpdatePower(in *usercenter.RoleUpdatePowerReq) (*usercenter.RoleUpdatePowerResp, error) {
	err := l.roleDomain.RoleUpdatePowerExiste(in.RoleId, in.NewPowerId)
	if err != nil {
		return nil, err
	}
	err = l.roleDomain.RoleUpdatePower(in.RoleId, in.OldPowerId, in.NewPowerId)
	if err != nil {
		return nil, err
	}
	return &usercenter.RoleUpdatePowerResp{}, nil
}

func (l *RoleLogic) RoleList(in *usercenter.RoleListReq) (*usercenter.RoleListResp, error) {
	roleList, err := l.roleDomain.RoleList(in.Page)
	if err != nil {
		return nil, err
	}
	var resp []*usercenter.RoleBase
	if len(roleList) > 0 {
		for _, role := range roleList {
			resp = append(resp, &usercenter.RoleBase{
				Id:       role.Id,
				UpdateAt: role.UpdateAt.Unix(),
				CreateAt: role.CreateAt.Unix(),
				ParentId: role.ParentId,
				Weight:   role.Weight,
				Name:     role.Name,
			})
		}
	}

	return &usercenter.RoleListResp{
		RoleList: resp,
	}, nil
}

func (l *RoleLogic) RoleDetail(in *usercenter.RoleDetailReq) (*usercenter.RoleDetailResp, error) {
	role, err := l.roleDomain.RoleDetail(in.Id)
	if err != nil {
		return nil, err
	}

	return &usercenter.RoleDetailResp{
		RoleInfo: &usercenter.RoleBase{
			Id:       role.Id,
			Name:     role.Name,
			ParentId: role.ParentId,
			Weight:   role.Weight,
			CreateAt: role.CreateAt.Unix(),
			UpdateAt: role.UpdateAt.Unix(),
		},
	}, nil
}

func (l *RoleLogic) RoleGetPower(in *usercenter.RoleGetPowerReq) (*usercenter.RoleGetPowerResp, error) {
	powerList, err := l.roleDomain.RoleGetPower(in.Id)
	if err != nil {
		return nil, err
	}
	var resp []*usercenter.PowerBase
	if len(powerList) > 0 {
		for _, power := range powerList {
			resp = append(resp, &usercenter.PowerBase{
				Id:       power.Id,
				Name:     power.Name,
				Method:   power.Method,
				ParentId: power.ParentId,
				Url:      power.Url,
				CreateAt: power.CreateAt.Unix(),
				UpdateAt: power.UpdateAt.Unix(),
			})
		}
	}
	return &usercenter.RoleGetPowerResp{
		PowerList: resp,
	}, nil
}

func (l *RoleLogic) RoleGetUser(in *usercenter.RoleGetUserReq) (*usercenter.RoleGetUserResp, error) {
	userList, err := l.roleDomain.RoleGetUser(in.Id)
	if err != nil {
		return nil, err
	}
	var resp []*usercenter.UserBase
	if len(userList) > 0 {
		for _, user := range userList {
			resp = append(resp, &usercenter.UserBase{
				Id:       user.Id,
				Activity: user.Activity,
				Gender:   user.Gender,
				Country:  user.Country,
				Email:    user.Email,
				OpenId:   user.OpenId,
				Phone:    user.Phone,
				Name:     user.Name,
				CreateAt: user.CreateAt.Unix(),
				UpdateAt: user.UpdateAt.Unix(),
			})
		}
	}
	return &usercenter.RoleGetUserResp{
		UserList: resp,
	}, nil
}
